package cn.lili.controller.common;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.email.dto.EmailDTO;
import cn.lili.modules.email.service.EmailService;
import cn.lili.modules.verification.entity.enums.VerificationEnums;
import cn.lili.modules.verification.service.VerificationService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 邮箱接口
 *
 * @author moxingjian
 * @version 1.0
 * @date 2023-10-19 15:36
 * <p>
 * 版本更新：
 * 更新版本号    更新人      更新时间      更新内容
 */
@Slf4j
@RestController
@RequestMapping("/common/common/email")
public class EmailController {


    @Resource
    private EmailService emailService;
    @Resource
    private VerificationService verificationService;

    /**
     * 邮箱验证码 每个邮箱一分钟只能一次 有效期10分钟
     *
     * @param email
     * @return
     * @throws IOException
     */
    @ApiOperation("发送邮箱验证码，每个邮箱一分钟只能一次")
    @GetMapping("/{verificationEnums}/{email}")
    public ResultMessage<String> sendEmailCode(@RequestHeader String uuid,
                                              @PathVariable String email,
                                              @PathVariable VerificationEnums verificationEnums) {
        // 校验
        verificationService.check(uuid, verificationEnums);
        // 发送邮件验证码
        emailService.sendEmailCode(email, verificationEnums, uuid);
        return ResultUtil.success(ResultCode.VERIFICATION_EMAIL_SEND_SUCCESS);
    }

    @PostMapping("/send-by-member-id")
    public ResultMessage<Void> sendEmailByMemberId(@Valid @RequestBody EmailDTO emailDTO) {
        emailService.sendEmailByMemberId(emailDTO);
        return ResultUtil.success();
    }

    @PostMapping("/send-by-store-id")
    public ResultMessage<Void> sendEmailByStoreId(@Valid @RequestBody EmailDTO emailDTO) {
        emailService.sendEmailByStoreId(emailDTO);
        return ResultUtil.success();
    }

}
